package com.haiwei.poi.word.loop;

import com.haiwei.poi.common.HaiweiStringUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * loop 解析器.
 *
 * @date: 2022-06-26
 * @version: 1.0
 * @author: karry@haiwei.com
 */
public class HaiweiLoopSginParser {
    
    private static final String LOOP_END = "##={end}";
    
    /**
     * ^#{2,}=\{begin:([^_|.]{1,})_([^_|.]{1,})\}
     * ##={begin:items_item}
     */
    private static Pattern LOOP_SIGN_START_PATTERN = Pattern.compile("^#{2,}=\\{begin:([^_]{1,})_([^_]{1,})\\}");
    
    /**
     * 循环排序规则
     * #{2,}=\{order:([^_]{1,})_([^_]{1,})\}
     * ##={order:item.time_升序}
     */
    private static Pattern LOOP_SIGN_ORDER_PATTERN = Pattern.compile("#{2,}=\\{order:([^_]{1,})_([^_]{1,})\\}");
    
    
    public static String LOOP_SIGN_SEARCH = "search";
    public static String LOOP_SIGN_EXTEND = "extend";
    public static String LOOP_SIGN_REFERINFO = "referInfo";
    
    private static Pattern LOOP_SIGN_SEARCH_PATTERN = Pattern.compile("#{2,}=\\{search:(.{1,})\\}");
    
    private static Pattern LOOP_SIGN_EXTEND_PATTERN = Pattern.compile("#{2,}=\\{extend:(.{1,})\\}");
    
    private static Pattern LOOP_SIGN_REFERINFO_PATTERN = Pattern.compile("#{2,}=\\{referInfo:(.{1,})\\}");
    
    
    /**
     * 根据文本判断是否是loop标签
     * 示例：##={begin:items_item}##={order:item.time_升序/降序}
     * @param text
     * @return
     */
    public static boolean isLoopStartSign(String text){
        if(HaiweiStringUtil.isEmpty(text)){
            return false;
        }
        return LOOP_SIGN_START_PATTERN.matcher(text).find();
    }
    
    /**
     * 判断是否是Loop End标签
     * @param text
     * @return
     */
    public static boolean isLoopEndSign(String text){
        return LOOP_END.equalsIgnoreCase(text);
    }
    
    /**
     * 解析为Loop标签
     * ####={begin:order.items_item}
     * @param text
     * @return
     */
    public static HaiweiLoopSign parse(String text){
        if(HaiweiStringUtil.isEmpty(text)){
            return null;
        }
    
        HaiweiLoopSign loopSign = new HaiweiLoopSign();
        
        Matcher matcher = LOOP_SIGN_START_PATTERN.matcher(text);
        if(matcher.find() && matcher.groupCount() == 2){
            // order.items
            loopSign.setPath(matcher.group(1));
            // item
            loopSign.setItem(matcher.group(2));
        }
    
        matcher = LOOP_SIGN_ORDER_PATTERN.matcher(text);
        if(matcher.find() && matcher.groupCount() == 2){
            /**
             * 注意：order item, 如：name，account,updateTime
             * 不可以是：item.name
             */
            loopSign.setOrderItem(matcher.group(1));
            // 排序规则
            loopSign.setOrderRule(HaiweiLoopOrderEnum.value(matcher.group(2)));
        }
    
        matcher = LOOP_SIGN_SEARCH_PATTERN.matcher(text);
        if(matcher.find() && matcher.groupCount() == 1){
            loopSign.getParams().put(LOOP_SIGN_SEARCH,matcher.group(1));
        }
    
        matcher = LOOP_SIGN_EXTEND_PATTERN.matcher(text);
        if(matcher.find() && matcher.groupCount() == 1){
            loopSign.getParams().put(LOOP_SIGN_EXTEND,matcher.group(1));
        }
    
        matcher = LOOP_SIGN_REFERINFO_PATTERN.matcher(text);
        if(matcher.find() && matcher.groupCount() == 1){
            loopSign.getParams().put(LOOP_SIGN_REFERINFO,matcher.group(1));
        }
    
        return loopSign;
    }
    
    
    
    
}
